package com.haha.gulimall.ware.service.impl;

import com.haha.common.constant.WareConstant;
import com.haha.gulimall.ware.entity.PurchaseDetailEntity;
import com.haha.gulimall.ware.service.PurchaseDetailService;
import com.haha.gulimall.ware.service.WareSkuService;
import com.haha.gulimall.ware.vo.MergeVo;
import com.haha.gulimall.ware.vo.PurchaseDoneVo;
import com.haha.gulimall.ware.vo.PurchaseItemDownVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.Query;

import com.haha.gulimall.ware.dao.PurchaseDao;
import com.haha.gulimall.ware.entity.PurchaseEntity;
import com.haha.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Resource
    PurchaseDetailServiceImpl purchaseDetailService;
    @Resource
    WareSkuService wareSkuService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询未领取的采购单
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> qw = new QueryWrapper<>();
        qw.eq("status",0).or().eq("status",1);
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
               qw
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void merge(MergeVo mergeVo) {
            Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId == null) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATE.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = mergeVo.getItems().stream().filter(haha->{
            Integer status = purchaseDetailService.getById(haha).getStatus();
            if(status==WareConstant.PurchaseDetailStatusEnum.CREATE.getCode()) return true;
            else return false;
        }).map(item -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(item);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGN.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
        //更新采购单的更新时间
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setId(purchaseId);
        this.updateById(purchaseEntity);

    }

    /**
     * 领取采购单
     */
    @Override
    public void receivePurchase(List<Long> ids) {
        List<PurchaseEntity> collect = ids.stream().map(id -> {
            PurchaseEntity byId = this.getById(id);
            return byId;
        }).filter(item -> {
            if (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATE.getCode()
                    || item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGN.getCode()) {
                return true;
            }
            return false;
        }).map(item->{
            item.setStatus(WareConstant.PurchaseStatusEnum.RECIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);   //更新了采购单的状态；

        //更新采购单中采购物品的状态
        collect.forEach(item->{
            List<PurchaseDetailEntity> entities = purchaseDetailService.getDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> detailEntities = entities.stream().map(obj -> {
                PurchaseDetailEntity entity = new PurchaseDetailEntity();
                entity.setId(obj.getId());
                entity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return entity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(detailEntities);
        });

    }

    @Override
    @Transactional
    public void overPurchase(PurchaseItemDownVo purchaseItemDownVo) {
        Boolean flag=true;
        List<PurchaseDetailEntity> update = new ArrayList<>();
        for (PurchaseDoneVo item : purchaseItemDownVo.getItems()) {
            PurchaseDetailEntity entity = new PurchaseDetailEntity();
            if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
                flag = false;
            }
            else{
                PurchaseDetailEntity detailEntity = purchaseDetailService.getById(item.getItemId());
                //add stock
                if(detailEntity.getStatus()==WareConstant.PurchaseDetailStatusEnum.BUYING.getCode() ){
                    wareSkuService.addStock(detailEntity.getSkuId(),detailEntity.getWareId(),detailEntity.getSkuNum());
                }
             }
            entity.setStatus(item.getStatus());
            entity.setId(item.getItemId());
            update.add(entity);
        }
        purchaseDetailService.updateBatchById(update);   //更新采购项状态

        //更新采购单
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseItemDownVo.getId());
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setStatus(flag?WareConstant.PurchaseStatusEnum.FINSH.getCode() : WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        this.updateById(purchaseEntity);

    }

}